﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary.BLL;
using CommonLibrary.Model;

namespace SmsSeries
{
    public partial class frmComOption : Form
    {
        public frmComOption()
        {
            InitializeComponent();
        }
        List<ComboBox> listComNo=new List<ComboBox>();
        List<ComboBox> listComProperty = new List<ComboBox>();
        List<TextBox> listTelNumber = new List<TextBox>();
        List<NumericUpDown> listLimitedCount=new List<NumericUpDown>();
        List<CheckBox> listEnabled=new List<CheckBox>();

        List<ComConfigInfo> comConfigInfos = new List<ComConfigInfo>();

        //窗体加载
        private void frmComOption_Load(object sender, EventArgs e)
        {
            comConfigInfos = ComConfig.GetList("");

            #region List里面添加控件

            for (int j = 1; j < 31; j++)
            {
                comboBox1.Items.Add(j.ToString());
                comboBox4.Items.Add(j.ToString());
                comboBox6.Items.Add(j.ToString());
                comboBox8.Items.Add(j.ToString());
                comboBox10.Items.Add(j.ToString());
                comboBox12.Items.Add(j.ToString());
                comboBox14.Items.Add(j.ToString());
                comboBox16.Items.Add(j.ToString());
                comboBox18.Items.Add(j.ToString());
                comboBox20.Items.Add(j.ToString());
                comboBox22.Items.Add(j.ToString());
                comboBox24.Items.Add(j.ToString());
                comboBox26.Items.Add(j.ToString());
                comboBox28.Items.Add(j.ToString());
                comboBox30.Items.Add(j.ToString());
                comboBox32.Items.Add(j.ToString());
            }

            listComNo.Add(comboBox1);
            listComNo.Add(comboBox4);
            listComNo.Add(comboBox6);
            listComNo.Add(comboBox8);
            listComNo.Add(comboBox10);
            listComNo.Add(comboBox12);
            listComNo.Add(comboBox14);
            listComNo.Add(comboBox16);
            listComNo.Add(comboBox18);
            listComNo.Add(comboBox20);
            listComNo.Add(comboBox22);
            listComNo.Add(comboBox24);
            listComNo.Add(comboBox26);
            listComNo.Add(comboBox28);
            listComNo.Add(comboBox30);
            listComNo.Add(comboBox32);

            listComProperty.Add(comboBox2);
            listComProperty.Add(comboBox3);
            listComProperty.Add(comboBox5);
            listComProperty.Add(comboBox7);
            listComProperty.Add(comboBox9);
            listComProperty.Add(comboBox11);
            listComProperty.Add(comboBox13);
            listComProperty.Add(comboBox15);
            listComProperty.Add(comboBox17);
            listComProperty.Add(comboBox19);
            listComProperty.Add(comboBox21);
            listComProperty.Add(comboBox23);
            listComProperty.Add(comboBox25);
            listComProperty.Add(comboBox27);
            listComProperty.Add(comboBox29);
            listComProperty.Add(comboBox31);

            foreach (ComboBox comboBox in listComNo)
            {
                comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            }

            foreach (ComboBox comboBox in listComProperty)
            {
                comboBox.Items.Add("9600,n,8,1");
                comboBox.Items.Add("19200,n,8,1");
                comboBox.Items.Add("38400,n,8,1");
                comboBox.Items.Add("57600,n,8,1");
                comboBox.Items.Add("115200,n,8,1");

                comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            }

            listTelNumber.Add(textBox1);
            listTelNumber.Add(textBox2);
            listTelNumber.Add(textBox3);
            listTelNumber.Add(textBox4);
            listTelNumber.Add(textBox5);
            listTelNumber.Add(textBox6);
            listTelNumber.Add(textBox7);
            listTelNumber.Add(textBox8);
            listTelNumber.Add(textBox9);
            listTelNumber.Add(textBox10);
            listTelNumber.Add(textBox11);
            listTelNumber.Add(textBox12);
            listTelNumber.Add(textBox13);
            listTelNumber.Add(textBox14);
            listTelNumber.Add(textBox15);
            listTelNumber.Add(textBox16);

            listLimitedCount.Add(numericUpDown1);
            listLimitedCount.Add(numericUpDown2);
            listLimitedCount.Add(numericUpDown3);
            listLimitedCount.Add(numericUpDown4);
            listLimitedCount.Add(numericUpDown5);
            listLimitedCount.Add(numericUpDown6);
            listLimitedCount.Add(numericUpDown7);
            listLimitedCount.Add(numericUpDown8);
            listLimitedCount.Add(numericUpDown9);
            listLimitedCount.Add(numericUpDown10);
            listLimitedCount.Add(numericUpDown11);
            listLimitedCount.Add(numericUpDown12);
            listLimitedCount.Add(numericUpDown13);
            listLimitedCount.Add(numericUpDown14);
            listLimitedCount.Add(numericUpDown15);
            listLimitedCount.Add(numericUpDown16);

            foreach (NumericUpDown numericUpDown in listLimitedCount)
            {
                numericUpDown.Maximum = 100000;
            }

            listEnabled.Add(checkBox1);
            listEnabled.Add(checkBox2);
            listEnabled.Add(checkBox3);
            listEnabled.Add(checkBox4);
            listEnabled.Add(checkBox5);
            listEnabled.Add(checkBox6);
            listEnabled.Add(checkBox7);
            listEnabled.Add(checkBox8);
            listEnabled.Add(checkBox9);
            listEnabled.Add(checkBox10);
            listEnabled.Add(checkBox11);
            listEnabled.Add(checkBox12);
            listEnabled.Add(checkBox13);
            listEnabled.Add(checkBox14);
            listEnabled.Add(checkBox15);
            listEnabled.Add(checkBox16);

            #endregion

            int i = 0;
            foreach (ComConfigInfo comConfigInfo in comConfigInfos)
            {
                listComNo[i].Text = comConfigInfo.ComNo.ToString();
                listComProperty[i].Text = comConfigInfo.ComProperty;
                listTelNumber[i].Text = comConfigInfo.TelNnumber;
                listTelNumber[i].Tag = comConfigInfo.ComID+","+i;
                listLimitedCount[i].Text = comConfigInfo.LimitedCount.ToString();
                listEnabled[i].Checked = comConfigInfo.Enabled;
                i++;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (TextBox textBox in listTelNumber)
            {
                if (textBox.Tag != null && textBox.Tag.ToString() != "")
                {
                    int comID =Convert.ToInt32( textBox.Tag.ToString().Substring(0, textBox.Tag.ToString().IndexOf(",")));
                    int index = Convert.ToInt32(textBox.Tag.ToString().Substring(textBox.Tag.ToString().IndexOf(",") + 1)); 
                        
                    ComConfigInfo comConfigInfo = comConfigInfos.Where(o => o.ComID == comID).FirstOrDefault();
                    if (comConfigInfo.ComNo.ToString() != listComNo[index].Text
                    || comConfigInfo.ComProperty != listComProperty[index].Text
                    || comConfigInfo.TelNnumber != listTelNumber[index].Text
                    || comConfigInfo.LimitedCount.ToString() != listLimitedCount[index].Text
                    || comConfigInfo.Enabled != listEnabled[index].Checked)
                    {
                        ComConfigInfo _comConfigInfo=new ComConfigInfo(
                            comConfigInfo.ComID,Convert.ToInt32(listComNo[index].Text),
                            listComProperty[index].Text,listTelNumber[index].Text,
                            Convert.ToInt32(listLimitedCount[index].Text),
                            listEnabled[index].Checked
                            );
                        ComConfig.UpdateNoSendedCount(_comConfigInfo);
                    }
                    i++;
                }
                else
                {
                    if (listComNo[i].Text != "")
                    {
                         ComConfigInfo _comConfigInfo=new ComConfigInfo(
                           Convert.ToInt32(listComNo[i].Text),
                            listComProperty[i].Text,listTelNumber[i].Text,
                            Convert.ToInt32(listLimitedCount[i].Text),0,
                            listEnabled[i].Checked
                            );
                        ComConfig.Insert(_comConfigInfo);
                    }

                    i++;
                }
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (TextBox textBox in listTelNumber)
            {
                if (textBox.Tag != null && textBox.Tag.ToString() != "")
                {
                    int comID = Convert.ToInt32(textBox.Tag.ToString().Substring(0, textBox.Tag.ToString().IndexOf(",")));
                    int index = Convert.ToInt32(textBox.Tag.ToString().Substring(textBox.Tag.ToString().IndexOf(",") + 1));

                    ComConfigInfo comConfigInfo = comConfigInfos.Where(o => o.ComID == comID).FirstOrDefault();
                    if (comConfigInfo.ComNo.ToString() != listComNo[index].Text
                    || comConfigInfo.ComProperty != listComProperty[index].Text
                    || comConfigInfo.TelNnumber != listTelNumber[index].Text
                    || comConfigInfo.LimitedCount.ToString() != listLimitedCount[index].Text
                    || comConfigInfo.Enabled != listEnabled[index].Checked)
                    {
                        ComConfigInfo _comConfigInfo = new ComConfigInfo(
                            comConfigInfo.ComID, Convert.ToInt32(listComNo[index].Text),
                            listComProperty[index].Text, listTelNumber[index].Text,
                            Convert.ToInt32(listLimitedCount[index].Text),
                            listEnabled[index].Checked
                            );
                        ComConfig.UpdateNoSendedCount(_comConfigInfo);
                    }
                    i++;
                }
                else
                {
                    if (listComNo[i].Text != "")
                    {
                        ComConfigInfo _comConfigInfo = new ComConfigInfo(
                          Convert.ToInt32(listComNo[i].Text),
                           listComProperty[i].Text, listTelNumber[i].Text,
                           Convert.ToInt32(listLimitedCount[i].Text), 0,
                           listEnabled[i].Checked
                           );
                        ComConfig.Insert(_comConfigInfo);
                    }

                    i++;
                }
            }
            Close();
        }
    }
}
